<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\api\model\Order as OrderModel;
use app\api\model\UserAddress as UserAddressModel;
use think\Db;
use think\Request;


class Order extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '';

    /**
     * 提交订单
     *
     */
    public function buy()
    {
        $uid  = $this->auth->id;

        $cart_id    = $this->request->post('cart_id');       //购物id
        $item_id    = $this->request->post('item_id');       //规格id
        $type    = $this->request->post('type',0);    //购买类型0立即下单,1购物车
        $goods_num  = $this->request->post('goods_num');     // 购买数量

        $address_type = $this->request->post('address_type',0); //地址类型 0:用户地址 1= 安装店地址
        $address_id   = $this->request->post('address_id'); //地址id

        $pay_way      = $this->request->post('pay_way');    // 支付方式 0:微信

        if (!$pay_way)
        {
            $this->success('请选择支付方式', '', 401);
        }

        $use_integral = $this->request->post('use_integral',1);  // 是否使用积分;0-不使用;1-使用

        $share_id     = $this->request->post('share_id',0); //分享者id
        $collocation  = $this->request->post('collocation',0); //搭配id
        $money  = $this->request->post('money',0); //搭配id

        $order = OrderModel::add($uid, $cart_id, $item_id, $goods_num,$collocation,$type,$money); //返回支付信息
        // dump($order);
        // die;

        if ($order['status'] == false)
        {
            $this->success($order['msg'],'',400);
        }

        if (!$address_id)
        {
            $this->success('请填写收货地址','',400);
        }

        $UserAddressModel = new UserAddressModel();
        // 获取用户地址
        if ($address_type == 1)
        {
            // 安装店
            $res = $UserAddressModel->getshopaddress($address_id);
            if (!$res) $this->success('地址信息不存在','',400);
            $address = [
                'name'    => $res['shop_name'],
                'phone'   => $res['phone'],
                'province'=> $res['province'],
                'coty'    => $res['city'],
                'area'    => $res['area'],
                'address' => $res['address'],
                'is_shop' => $res['id'],
            ];
        }else{
            // 用户
            $res = $UserAddressModel->getAddress($address_id);
            if (!$res) $this->success('地址信息不存在','',400);
            $address = [
                'name'    => $res['name'],
                'phone'   => $res['phone'],
                'province' => $res['province'],
                'coty'    => $res['coty'],
                'area'    => $res['area'],
                'address' => $res['address'],
                'is_shop' => 0
            ];
        }

        // 订单支付
        $response = OrderModel::Orderadd($uid, $order, $address, $share_id, $use_integral,$cart_id);
        if ($response['status'])
        {
            $this->success('提交成功',$response['data'],200);
        }else{
            $this->success($response['msg'],'',400);
        }
    }
    /**
     * 充值金额
     */
    public function Userbuy()
    {
        $uid = $this->auth->id;
        $id = $this->request->post('id');
        if (!$uid) $this->success('未登录', '', 401);
        if (!$id)  $this->success('选择充值金额', '', 400);
        $userorder = OrderModel::Orderuser($uid, $id);

        if ($userorder['status'])
        {
            $this->success($userorder['msg'], $userorder['data'], 200);
        }else{
            $this->success($userorder['msg'], '', 400);
        }
    }
    /**
     * 订单列表
     */
    public function orderlist()
    {
        $uid = $this->auth->id;
        $name = $this->request->post('name',''); // 搜索
        $type = $this->request->post('type','0'); // 类型:0全部.1待付款.2待发货.3待收货.4待评价.5退款
        $userModel = new \app\api\model\User();
        $user = $userModel->getUser($uid);
        if (!$user) $this->success('没有此用户', '', 401);

        $orderlist = OrderModel::OrderList($name, $type, $uid);

        if ($orderlist)
        {
            $this->success('获取成功',$orderlist,200);
        }else{
            $this->success('该订单不存在','',200);
        }
    }
    /**
     * 修改订单地址
     */
    public function orderaddress()
    {
        $id = $this->request->post('id');
        $address_id = $this->request->post('address_id');
        $type = $this->request->post('type');
        $res  = OrderModel::upaddress($id, $address_id,$type);

        if ($res)
        {
            $this->success('修改成功','',200);
        }else{
            $this->success('修改失败','',400);
        }
    }
    /**
     * 取消订单
     */
    public function cancelorder()
    {
        $id = $this->request->post('id');
        $uid = $this->auth->id;
        $response = OrderModel::cancel($id, $uid);
        if ($response['status'])
        {
            $this->success($response['msg'],'', 200);
        }else{
            $this->success($response['msg'],'', 400);
        }
    }
    /**
     * 提醒发货
     */
    public function remindorder()
    {
        $id = $this->request->post('id');
        $uid = $this->auth->id;
        $response = OrderModel::remind($id, $uid);
        if ($response['status'])
        {
            $this->success($response['msg'],'', 200);
        }else{
            $this->success($response['msg'],'', 400);
        }
    }
    /**
     * 订单详情
     */
    public function orderdetails()
    {
        $uid = $this->auth->id;
        $id = $this->request->post('id');
        $lng = $this->request->post('lng'); // 经度
        $lat = $this->request->post('lat'); // 纬度
        $response = OrderModel::getorder($id, $uid, $lng, $lat);
        if ($response)
        {
            $this->success('查询成功',$response, 200);
        }else{
            $this->success('查询失败','', 400);
        }
    }
    /**
     * 确认收货
     */
    public function usertakegoods()
    {
        $uid = $this->auth->id;
        $id = $this->request->post('id'); //订单ID
        if (!$id)
        {
            $this->success('请选择订单','', 400);
        }
        $response = OrderModel::take_goods($uid, $id);
        if ($response['status'])
        {
            $this->success($response['msg'],'', 200);
        }else{
            $this->success($response['msg'],'', 400);
        }

    }
    /**
     * 删除订单
     */
    public function delorder()
    {
        $uid = $this->auth->id;
        $id = $this->request->post('id');
        if (!$id) $this->success('缺少参数','',400);
        $order = OrderModel::get($id);
        if (!$order)
        {
            $this->success('订单不存在','',400);
        }
        if ($order['uid'] != $uid)
        {
            $this->success('该订单无法操作','',400);
        }
        $order->del = 1;
        $order->update_time = time();
        $response = $order->save();
        if ($response)
        {
            $this->success('删除成功',$response,200);
        }else{
            $this->success('删除失败','',200);
        }
    }
    /**
     * 延长收货
     */
    public function extendorder()
    {
        $uid = $this->auth->id;
        $id = $this->request->param('id');
        if (empty($id)) $this->success('缺失参数', '', 400);
        $order = OrderModel::get($id);
        if (!$order) $this->success('订单不存在', '', 400);
        if ($order['uid'] != $uid) $this->success('该订单无法操作', '', 400);
        if ($order['day_time'])
        {
            $this->success('无法重复操作','',400);
        }
        $number = 3600 * 24 * 3;
        $order->day_time = 3;
        $order->end_time = $number + (int)$order['end_time'];
        $order->save();
        if ($order->id)
        {
            $this->success('修改成功', $order->id, 200);
        }else{
            $this->success('暂无法操作','',400);
        }
    }

    public function orderNum()
    {
        $uid  = $this->auth->id;

        $user = new \app\api\model\User();
        $userInfo = $user->getUser($uid);

        if (empty($userInfo))
        {
            $this->success('暂无用户','',400);
        }

        $orderModel = new OrderModel();

        $where = [];
        $where['uid'] = ['eq',$uid];
        $orderList = [];
        $orderList['dfk_payment'] = $orderModel->where($where)->where('order_status',0)->count();
        $orderList['dfh_payment'] = $orderModel->where($where)->where('order_status',1)->count();
        $orderList['dsh_payment'] = $orderModel->where($where)->where('order_status',2)->count();
        $orderList['dpj_payment'] = $orderModel->where($where)->where('order_status',3)->count();
        $orderList['sh_payment']  = $orderModel->where($where)->where('order_status',5)->where('pay_status','<','2')->count();

        $this->success('获取成功',$orderList,200);
    }
}